package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.commen.LabelVlalue;
import com.itheima.commen.ReturnSuccess;
import com.itheima.dto.PageComDto;
import com.itheima.pojo.Subjects;
import com.itheima.pojo.SubjectsTags;
import com.itheima.service.SubjectsService;
import com.itheima.service.SubjectsTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 标签
 */

@RestController
@RequestMapping("/tags")
public class SubjectsTagsController {

    @Autowired
    private SubjectsTagsService subjectsTagsService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SubjectsService subjectsService;

    /**
     * 标签添加
     */
    @PostMapping
    public Integer add(@RequestBody SubjectsTags subjectsTags){
        //todo 待完善,从seeion中获取userId
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute("userId");
        String jsonString = JSON.toJSONString(userId);
        Integer uid = JSON.parseObject(jsonString, Integer.class);
        subjectsTags.setAddDate(new Date());
        subjectsTags.setState(1);
        subjectsTags.setCreatorId(uid);
        subjectsTags.setTotals(0);
        subjectsTagsService.save(subjectsTags);
        LambdaQueryWrapper<Subjects> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Subjects::getId,subjectsTags.getSubjectID());
        Subjects subjects = subjectsService.getOne(wrapper);
        subjects.setTags(subjects.getTags()+1);
        subjectsService.updateById(subjects);
        return subjectsTags.getId();
    }
    /**
     * 标签修改
     */
    @PutMapping("/{id}")
    public ReturnSuccess update(@RequestBody SubjectsTags subjectsTags, @PathVariable Integer id){
        if(subjectsTags==null){
            return ReturnSuccess.error();
        }
        subjectsTags.setId(id);
        subjectsTagsService.updateById(subjectsTags);
        return ReturnSuccess.success();
    }
    /**
     * 标签列表
     */
    @GetMapping
    public PageComDto findPage(Integer page, Integer pagesize, String tagName, Integer state){
        return subjectsTagsService.findPage(page,pagesize, tagName, state);
    }

    /**
     * 标签删除
     */
    @DeleteMapping("/{id}")
    public ReturnSuccess delete(@PathVariable Integer id){
        subjectsTagsService.removeById(id);
        return ReturnSuccess.success();
    }
    /**
     * 标签状态
     */
    @PostMapping("/{id}/{state}")
    public ReturnSuccess updateByState(@PathVariable Integer id, @PathVariable Integer state){
        LambdaUpdateWrapper<SubjectsTags> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SubjectsTags::getId,id).set(SubjectsTags::getState,state);
        subjectsTagsService.update(wrapper);
        return ReturnSuccess.success();
    }
    /**
     * 标签详情
     */
    @GetMapping("/{id}")
    public SubjectsTags findById(@PathVariable Integer id){
        return subjectsTagsService.getById(id);
    }
    /**
     * 试题录入
     */
    @GetMapping("/simple")
    public List<LabelVlalue> findBySub(Integer subjectID){
        LambdaQueryWrapper<SubjectsTags> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubjectsTags::getSubjectID,subjectID);
        List<SubjectsTags> list = subjectsTagsService.list(wrapper);
        ArrayList<LabelVlalue> list1 = new ArrayList();
        for (SubjectsTags subjectsTags : list) {
            String tagName = subjectsTags.getTagName();
            Integer id = subjectsTags.getId();
            LabelVlalue labelVlalue = new LabelVlalue();
            labelVlalue.setLabel(tagName);
            labelVlalue.setValue(id);
            list1.add(labelVlalue);
        }
        return list1;
    }
}
